<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('online_users', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id')->comment('用户ID');
            $table->string('username')->comment('用户名');
            $table->string('token')->unique()->comment('Token');
            $table->string('ip')->nullable()->comment('IP地址');
            $table->text('user_agent')->nullable()->comment('用户代理');
            $table->timestamp('login_at')->useCurrent()->comment('登录时间');
            $table->timestamp('last_activity_at')->useCurrent()->comment('最后活动时间');
            $table->timestamps();

            $table->index('user_id');
            $table->index('token');
            $table->index('last_activity_at');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('online_users');
    }
};

